(()=> {
	let ele = {};
	ele.explian = $('#explian');
	ele.modal_explian = $('#modal-explian');
	ele.close_explian = $('#close-explian');
	ele.modal_tips = $('#modal-tips');
	ele.close_tips = $('#close-tips');
	ele.modal_info = $('#modal-info');
	ele.close_info = $('#close-info');
	ele.info = $('#info');
	ele.yiGain = $('#yiGain');
	ele.myFriend = $('#myFriend');
	ele.yuji = $('#yuji');
	ele.jinri = $('#jinri');
	ele.leiji = $('#leiji');
	ele.topbtm = $('#topbtm');
	ele.modal_tips = $('#modal-tips');
	ele.close_tips = $('#close-tips');
	ele.jiesuo = $('#jiesuo');
	ele.all_jiesuo = $('#all_jiesuo');
	ele.isall1 = $('#isall1');
	ele.isall2 = $('#isall2');
	ele.qudao = $('#qudao');
	
	//打开 玩法介绍弹窗
	ele.explian.onclick = () => {
		openMdl(ele.modal_explian);
	}
	//关闭 玩法介绍弹窗
	ele.close_explian.onclick = () => {
		closeMdl(ele.modal_explian);
	}
	//关闭 储存罐弹窗
	ele.close_tips.onclick = () => {
		closeMdl(ele.modal_tips);
	}
	// //打开 我的邀请人
	// ele.info.onclick = () => {
	// 	let giveid = ele.giveid.value;
	// 	if(!giveid) {
	// 		openTips('请输入正确的好友ID')
	// 	} else searchUser(giveid);
	// }
	//关闭 我的邀请人
	ele.close_info.onclick = () => {
		closeMdl(ele.modal_info);
	}
	// 渠道收益  入口
	
	
	
	let is_qu = '';
	
	//打开 储存罐弹窗
	let boxs = $('.box');
	for(let i = 1; i < boxs.length; i++) {
		boxs[i].onclick = ()=> {
			openMdl(ele.modal_tips);
		}
	}
	
	//关闭 储存罐弹窗
	ele.close_tips.onclick = () => {
		closeMdl(ele.modal_tips);
	}
	
	bagIndex();
	
	var dt = 365 * 24 * 60 * 60;
	
	
	// 我的钱包
	function bagIndex() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.BAG_INDEX,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					setCookie("p_invite", res.data.detail.my_invitation, dt)
					is_qu = res.data.detail.no_qd;
					ele.leiji.innerHTML = res.data.detail.all_money
					ele.myFriend.innerHTML = '我的好友：' + res.data.detail.friend_num + '人>'
					ele.jinri.innerHTML = res.data.detail.today_money
					// ele.yuji.innerHTML = res.data.detail.friend_cc
					ele.jiesuo.innerHTML = res.data.detail.need_gold
					ele.all_jiesuo.innerHTML = res.data.detail.all_gold
					ele.yiGain.innerHTML = '已获得<span>￥'+res.data.detail.has_money+'</span><br />达到￥'+res.data.list[0].money+'<br />可领取至钱包'
					// openTips(res.msg);
					drawIndexlist(res.data.list)
				} else openTips(res.msg || '未知错误');
				if(is_qu == 0) {
					ele.qudao.style.display = 'none';
				}else {
					ele.qudao.style.display = 'block';
					ele.qudao.onclick = () => {
						location.href = '../page/qudao.html'
					}
				}
			}
		});
	}
	
	
	function drawIndexlist(_data) {
		let html = '';
		let cls = '';
		let gao = '';
		let img = '';
		let spans = '';
		let is_have = '';
		for(let i = 0; i < _data.length; i++) {
			if(i == 0 || i == 4) {
				// if(_data[i].has_money == '') {
				// 	gao = 'box2 box3';
				// } else {
				// 	gao = 'box2 box3';
				// }
				gao = 'box2 box3';
			} else {
				gao = 'box2';
			}
			// clsass = i ==
			// console.log(_data[i].has_money === '');
			cls = _data[i].has_money === '' ? '' : '';
			is_have = _data[i].has_money !== '' ? 'yongyou' : '';
			img = _data[i].has_money === '' ? 'http://img.qxgs.cc/tiantianlaishouzu/img/makeMoney/jia' : 'http://img.qxgs.cc/tiantianlaishouzu/img/makeMoney/mi';
			spans = _data[i].has_money === '' ? '' : '<span>￥'+_data[i].has_money+'</span>'
			html += '<div class="box '+gao+ ' '+ is_have+'" data="'+_data[i].id+'">'
						+'<img src="'+img+'.png" alt="">'
						+spans
					+'</div>'
		}
		ele.topbtm.innerHTML = html;
		
		let box2s = $('.box2');
		for(let i = 0; i < box2s.length; i++) {
			box2s[i].onclick = ()=> {
				// let jiesuo = box2s[i].getAttribute('data');
				openMdl(ele.modal_tips);
			}
		}
		
		let yongyous = $('.yongyou');
		for(let i = 0; i < yongyous.length; i++) {
			yongyous[i].onclick = ()=> {
				let j = yongyous[i].getAttribute('data');
				getMoney(j)
			}
		}
		
		
	}
	
	ele.isall1.onclick = ()=> {
		bagBuy(0)
	}
	ele.isall2.onclick = ()=> {
		bagBuy(1)
	}
	// 开通钱包
	function bagBuy(is_all) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.BUG_BAG,
			method: 'POST',
			header: {
				'Authorization': token,
			},
			data: {
				is_all: is_all
			},
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					closeMdl(ele.modal_tips);
					bagIndex();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	// 领取钱包
	function getMoney(id) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.BGA_GET_MONEY,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				id: id
			},
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					bagIndex();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
})()